package com.atguigu.gulimall.order.config;

import com.atguigu.gulimall.order.entity.OrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 将ORDER_EVENT_EXCHANGE 交换机绑定延时队列，
 * 又是死信交换机又是普通交换机，绑定两个不同的队列
 *
 * @Author ShiJieLin
 * @Date 2022/4/29 12:26
 * @Version 1.0
 */
@Configuration
@Slf4j
public class GuliMallRabbitConfig {

    /** 交换机
     */
    public static final String ORDER_EVENT_EXCHANGE = "order.event.exchange";
    /** 普通队列
     */
    public static final String ORDER_RELEASE_QUEUE = "order.release.queue";
    /** 延时队列
     */
    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";
    /** 秒杀队列
     */
    public static final String ORDER_SECKILL_QUEUE = "order.seckill.queue";
    /** 延时队列路由键
     */
    public static final String DELAYED_ROUTING_KEY = "order.create.routingKey";
    /** 普通队列路由键
     */
    public static final String ORDER_RELEASE_ROUTING_KEY = "order.release.routingKey";
    /** 秒杀路由键
     */
    public static final String ORDER_SECKILL_ROUTING_KEY = "order.seckill.routingKey";

    /**
     * RabbitMQ发送接收消息的格式
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    /** 创建一个主题交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        Exchange exchange = ExchangeBuilder
                .topicExchange(ORDER_EVENT_EXCHANGE)
                .durable(true).build();
        return exchange;
    }
    /** 创建一个普通队列
     * @return
     */
    @Bean
    public Queue orderReleaseQueue() {
        return QueueBuilder.durable(ORDER_RELEASE_QUEUE).build();
    }
    /** 普通队列和普交换机的绑定
     * @return
     */
    @Bean
    public Binding releaseBinding() {
        Binding binding = new Binding(ORDER_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE, ORDER_RELEASE_ROUTING_KEY,null);
        return binding;
    }

    /** 创建一个延时队列
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 指定死信交换机
        arguments.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE);
        // 指定死信交换机的路由键
        arguments.put("x-dead-letter-routing-key", ORDER_RELEASE_ROUTING_KEY);
        // 消息的存活时间
        arguments.put("x-message-ttl", 60000);
        return QueueBuilder.durable(ORDER_DELAY_QUEUE).withArguments(arguments).build();
    }
    /** 延迟队列和交换机的绑定
     * @param orderDelayQueue
     * @param orderEventExchange
     * @return
     */
    @Bean
    public Binding delayedBinding(Queue orderDelayQueue, Exchange orderEventExchange){

        Binding binding = BindingBuilder.bind(orderDelayQueue)
                .to(orderEventExchange).with(DELAYED_ROUTING_KEY).noargs();

        return binding;
    }

    /** 将当前交换机，和库存服务的 解锁库存的队列进行绑定
     *  将当前订单被取消知晓，就发送消息到库存队列进行解锁
     * @return
     */
    @Bean
    public Binding orderReleaseOtherBinding() {

        Binding binding = new Binding("stock.release.queue",
                Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE, "order.release.other.#",null);
        return binding;
    }

    @Bean
    public Queue orderSeckillQueue(){
        return QueueBuilder.durable(ORDER_SECKILL_QUEUE).build();
    }

    @Bean
    public Binding orderSeckillBinding(){
        return new Binding(ORDER_SECKILL_QUEUE, Binding.DestinationType.QUEUE,
                ORDER_EVENT_EXCHANGE, ORDER_SECKILL_ROUTING_KEY, null);
    }

}
